﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using FiveStone.Service;
using FiveStone.MyStack;

namespace FiveStone
{
    public enum Player { Black = 1, White = 2, None = 0 };
    public enum GameWinner { 黑子胜利=1, 白子胜利=2, 平局=3,游戏开始=4,游戏中=5, None=6 };    

    /// <summary>
    /// 控制棋盘以及逻辑
    /// </summary>
    public class Boards
    {
        public bool PlayerIsPC = true;
        public Game game;
        public int[,] board = new int[15, 15];
        private Graphics mg;
        private Stones stone;
        private PC pc = new PC(false);
        private bool defaultChessFlag = true;//默认棋子颜色 true黑色 false白色
        private bool first = false;        
        private GameWinner winner = GameWinner.None;
        private bool isMePut = false;//是否我下棋
        public Player CurrentPlayer = Player.Black;
        private ListBox historyList;        

        public Boards(Graphics g)
        {
            mg = g;
            stone = new Stones(mg);            
        }

        public Boards(Graphics g, Game game1, ListBox list)
        {
            mg = g;
            stone = new Stones(mg);
            game = game1;
            game.st.CreatStack();
            historyList = list;
        }        

        public void Start(bool mflag)
        {
            ClearBoards();
            winner = GameWinner.游戏开始;
            if (!mflag)
            {
                defaultChessFlag = true;
                if(PlayerIsPC)//对方是否为电脑
                    PCPut();
            }
            else
            {
                defaultChessFlag = true;
                isMePut = true;
            }
            DrawBoard();
        }

        /// <summary>
        /// 清除棋盘
        /// </summary>
        public void ClearBoards()
        {
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    board[i, j] = -1;
                }
            }
        }

        /// <summary>
        /// 绘棋盘
        /// </summary>
        public void DrawBoard()
        {
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            Stream myStream = myAssembly.GetManifestResourceStream("FiveStone.board.png");
            Bitmap bt = new Bitmap(myStream);
            myStream.Close();
            mg.DrawImage(bt, 20, 20, bt.Width, bt.Height);

            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    if (board[i, j] == 0)
                    {
                        stone.DrawStone(i, j, true);
                    }
                    if (board[i, j] == 1)
                    {
                        stone.DrawStone(i, j, false);
                    }
                }
            }
            if (winner != GameWinner.None && winner != GameWinner.游戏中)
            {
                DrawString(winner.ToString());
            }
        }

        public void DrawString(string str)
        {
            mg.DrawString(str, new Font("黑体", 60, FontStyle.Bold), new SolidBrush(Color.LightCoral),150,250);//在矩形内画出字符串
        }

        /// <summary>
        /// 将棋子放到棋盘的x,y坐标
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public GameWinner PutDown(int x, int y)
        {
            GameWinner win = GameWinner.游戏中;
            if ((int)winner>3)
            {
                if (board[x, y] == -1)
                {
                    stone.DrawStone(x, y, defaultChessFlag);

                    if (defaultChessFlag)
                    {
                        board[x, y] = 0;
                    }
                    else
                    {
                        board[x, y] = 1;
                    }                    
                    game.st.Insert(x,y);                    

                    if (Rules.Winer(x, y, board) > 0)
                    {
                        switch (Rules.Winer(x, y, board))
                        {
                            case 1:
                                if (defaultChessFlag)
                                {
                                    first = !first;                                    
                                    DrawString("黑子胜利");
                                    win = GameWinner.黑子胜利;                                    
                                }
                                else
                                {
                                    first = !first;                                    
                                    win = GameWinner.白子胜利;
                                    DrawString("白子胜利");                                    
                                }
                                break;
                            case 2:
                                first = !first;                                
                                System.Windows.Forms.MessageBox.Show("平局");
                                DrawString("平局");
                                win = GameWinner.平局;
                                break;
                        }
                    }

                    defaultChessFlag = !defaultChessFlag;
                }
            }

            return win;
        }

        /// <summary>
        /// 玩家下棋
        /// 玩家下后，自动调用PC下棋
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void PersonPut(int x, int y)
        {
            PersonPut(x, y, null,false);
        }

        /// <summary>
        /// 玩家下棋
        /// 玩家下后，自动调用PC下棋
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="playerName"></param>
        public void PersonPut(int x, int y, string playerName,bool isSocketPut)
        {
            if (x < 680 && y < 680 && (isSocketPut || isMePut))//&& (isMePut || PlayerIsPC)
            {
                int m = (int)(x / 42);
                int n = (int)(y / 42);
                if (m < 0) { m = 0; }
                if (n < 0) { n = 0; }
                if (m > 14) { m = 14; }
                if (n > 14) { n = 14; }
                if (!Rules.IsExit(m, n, board))
                {
                    GameWinner win = PutDown(m, n);                    
                    if ((int)winner>3)
                    {
                        if (String.IsNullOrEmpty(playerName))
                        {
                            historyList.Items.Add(game.st.GetGobangRecord(User.MyName));
                            if(!PlayerIsPC)
                            {
                                NetProcessor.SendPut(x, y);
                                isMePut = false;
                            }                            
                        }
                        else
                        {
                            historyList.Items.Add(game.st.GetGobangRecord(playerName));
                            isMePut = true;
                        }
                        winner = win;
                        if (game.st.st.HistoryPoint.Count < 2)
                            DrawBoard();
                    }
                    if (PlayerIsPC && game.IsPC)
                    {
                        PCPut();
                        isMePut = true;
                    }
                }
            }
        }

        /// <summary>
        /// 电脑下棋
        /// 或网络对方下棋
        /// </summary>
        public void PCPut()
        {
            int m = 0, n = 0;
            int err = 0;            
            do
            {
                pc.Down(board);//AI计算下棋位置
                m = pc.X;
                n = pc.Y;
                err++;
                if (err > 100)
                {
                    System.Windows.Forms.MessageBox.Show("发生了一些错误，棋局将重新开始");
                    Start(true);
                }
            }
            while (Rules.IsExit(m, n, board));
            GameWinner win = PutDown(m, n);
            if ((int)winner>3)
            {
                historyList.Items.Add(game.st.GetGobangRecord(User.PlayerName));
                winner = win;
            }
            isMePut = true;
        }
    }

    public class Game
    {
        public MyStack.MyStack st = new FiveStone.MyStack.MyStack();
        public bool IsPC = true;
        //private Player CurrentPlay;
        private Player nextPlayer;
        private List<Player> players = new List<Player>();

        public Player JoinGame()
        {
            if (players.Count > 2)
                return Player.None;
            if (players.Count == 0)
            {
                IsPC = false;
                players.Add(Player.Black);
                return Player.Black;
            }
            if (players.Count == 1)
            {
                players.Add(Player.White);
                nextPlayer = Player.Black;
                return Player.White;
            }
            return Player.None;
        }        

    }

}
